﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using WisDom.SmartTMS.Data;
using WisDom.SmartTMS.Dtos;
using WisDom.SmartTMS.Entities;
using WisDom.SmartTMS.Enums;
using WisDom.SmartTMS.Interface;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Application.Services;

namespace WisDom.SmartTMS.Service
{
    public class ShipperAuditService :SmartTMSAppService, IShipperAuditService
    {
        //依赖注入
        private readonly IRepository<Shipper, Guid> _irepository;
        private readonly IBusinessAuditRepository _businessAudit;//审核仓储
        public ShipperAuditService(IRepository<Shipper, Guid> irepository, IBusinessAuditRepository businessAudit)
        {
            _irepository = irepository;
            _businessAudit = businessAudit;
        }

        /// <summary>
        /// 转批
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Task AuditAsync(AuditDto input)
        {
            //拿到审批主记录
            BusinessAudit auditInfo = _businessAudit.GetAuditById(input.AuditId, BusinessType.Shipper).SingleOrDefault();

            //当前审核人处理审批
            auditInfo.ProcessAudit((Guid)CurrentUser.Id, CurrentUser.Name, input.Remark,input.State);

            //持久化（更新主信息）
            _businessAudit.UpdateAsync(auditInfo);

            //新增下一个人审批日志
            auditInfo.AddAuditLogs((Guid)input.AuditId, GuidGenerator.Create(), input.CreatorRealName,input.State);

           return UnitOfWorkManager.Current.SaveChangesAsync();  //完成工作单元
            //return (auditInfo);
        }

        /// <summary>
        /// 终止审批（可能是审核通过或不通过）
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Task EndAuditAsync(AuditDto input)
        {
            BusinessAudit businessAudit = _businessAudit.GetAuditById(input.AuditId, BusinessType.Shipper).SingleOrDefault();
            //终止审批(input.State为通过或拒绝）
            businessAudit.EndAudit((Guid)CurrentUser.Id, CurrentUser.Name, input.State, input.Remark);
            //持久化
            _businessAudit.UpdateAsync(businessAudit);
            return UnitOfWorkManager.Current.SaveChangesAsync();  //完成工作单元
        }

        /// <summary>
        /// 查看审核记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<ShipperAuditDto>> GetShipperAuditAsync(PagedAndSortedResultRequestDto input)
        {
            var query = from s in _irepository
                        join b in _businessAudit.GetAuditList(BusinessType.Shipper) on s.Id equals b.Id
                        orderby s.Id
                        select new ShipperAuditDto
                        {
                            Id=s.Id,
                            ShipperName=s.ShipperName,
                            MobilePhoto=s.MobilePhoto,
                            CompanyName=s.CompanyName,
                            Address=s.Address,
                            LicenseValidity=s.LicenseValidity,
                            Remark=s.Remark,
                            LlicencePhoto=s.LlicencePhoto,
                            BusinessCode=b.BusinessCode,
                            BusinessTypeCode=b.BusinessTypeCode,
                            State=b.State,
                            AuditTime=DateTime.Now,
                            CreatorID=b.Id,
                            CreatorRealName=CurrentUser.UserName,
                           // auidtLogsDtos = ObjectMapper.Map<List<BusinessAuditLogs>, List<AuidtLogsDto>>(b.BusinessAuditLogs)
                        };
            //获取总条数
            var totalCount = await query.CountAsync();

            //从仓储获取实体数据对象
             List<ShipperAuditDto> vehicleAudits = await query.Skip(input.SkipCount).Take(input.MaxResultCount).ToListAsync();

            //返回分页结果集
             return new PagedResultDto<ShipperAuditDto>(totalCount, vehicleAudits);
        }
        /// <summary>
        /// 在审核日志表根据ID查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ShipperAuditDto> GetShipperAuditByIdAsync(Guid id)
        {
            //车辆表和审核表联查
            var query = from v in _irepository   //车辆信息
                        join a in _businessAudit.GetAuditById(id, BusinessType.Shipper) //单个车辆审核信息
                        on v.Id equals a.Id     //连接条件
                        select new ShipperAuditDto
                        {
                            Id=v.Id,
                            ShipperName = v.ShipperName,
                            MobilePhoto = v.MobilePhoto,
                            CompanyName = v.CompanyName,
                            Address = v.Address,
                            LicenseValidity = v.LicenseValidity,
                            Remark = v.Remark,
                            LlicencePhoto = v.LlicencePhoto,
                            BusinessCode = a.BusinessCode,
                            BusinessTypeCode = a.BusinessTypeCode,
                            State = a.State,
                            AuditTime = DateTime.Now,
                            CreatorID = a.Id,
                            CreatorRealName = CurrentUser.UserName,
                            //auidtLogsDtos = ObjectMapper.Map<List<BusinessAuditLogs>, List<AuidtLogsDto>>(a.BusinessAuditLogs)
                        };

            return await query.SingleOrDefaultAsync();  //返回
        }
    }
}
